// pages/post/detail.js
const ins = getApp()

Page({
  data: {
    post: null,
    comment: {
      list: [],
      page: {
        page: 1,
        offset: 10
      },
      hasNextPage: true,
      fetching: false,
      current_text: '',
      requesting: true,
    }
  },
  onLoad: function (options) {
    this.setData({
      request: wx.getStorageSync('request_detail'),
    }, () => {
      wx.removeStorageSync('request_detail')
      if(options.id == undefined || this.data.post == null || this.data.post.id != options.id) {
        this.fetchPost(options.id)
      } else {
        this.fetchComment()
      }
    })
  },
  previewImages(e) {
    wx.previewImage({
      urls: this.data.post.images,
      current: this.data.post.images[e.currentTarget.dataset.index]
    })
  },
  openLocation(e) {
    wx.openLocation({
      latitude: this.data.post.position.lat,
      longitude: this.data.post.position.lng,
      name: this.data.post.title,
      address: this.data.post.address
    })

    return false
  },
  fetchPost(id) {
    wx.showLoading({title: '请稍候', mask: true})

    ins.api.requestApi('/post/detail/' + id, 'GET', {}, {
      complete: () => {
        wx.hideLoading()
      },
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          this.setData({
            post: resp.data.data
          }, () => {
            this.fetchComment()
          })
        } else {
          wx.navigateBack({
            delta: 0,
            complete: () => {
              wx.showToast({
                title: resp.data.data != null ? resp.data.data[0] : resp.data.msg
              })
            }
          })
        }
      },
      fail: () => {
        wx.navigateBack({
          delta: 0,
          complete: () => {
            wx.showToast({
              title: '服务器错误',
              icon: 'error'
            })
          }
        })
      }
    })
  },
  toRequest() {
    wx.showLoading({title: '请稍候', mask: true})

    ins.api.requestApi('/request/commit/' + this.data.post.id, 'GET', {}, {
      complete: () => {
        wx.hideLoading()
      },
      success: (resp) => {
        wx.showToast({
          title: resp.data.msg,
          icon: 'none'
        })
      }
    })
  },
  fetchComment() {
    if(this.data.comment.fetching || !this.data.comment.hasNextPage) return false

    this.setData({'comment.fetching': true})
    ins.api.requestApi('post/comment/list', 'POST', {
      pid: this.data.post.id,
      page: this.data.comment.page,
    }, {
      complete: () => {
        this.setData({'comment.fetching': false})
      },
      success: (resp) => {
        const data = resp.data
        if(data.ret == ins.globalData.request_ret.SUCCESS) {
          if(data.data.length < this.data.comment.page.offset) {
            this.setData({'comment.hasNextPage': false})
          }

          if(data.data.length > 0) {
            this.setData({
              ['comment.list[' + this.data.comment.list.length + ']']: data.data,
              'comment.page.page': this.data.comment.page.page + 1
            })
          }
        }
      }
    })
  },
  commentContentChange(e) {
    this.setData({'comment.current_text': e.detail.value})
  },
  toComment() {
    if(this.data.comment.requesting || this.data.comment.current_text.length == 0) return false
    this.setData({'comment.requesting': true})
    wx.showLoading({title: '请稍候', mask: true})

    ins.api.requestApi('post/comment', 'POST', {
      pid: this.data.post.id,
      is_reply: false,
      content: this.data.comment.current_text
    }, {
      complete: () => {
        this.setData({'comment.requesting': false})
        wx.hideLoading()
      },
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          this.setData({
            ['comment.list[' + this.data.comment.list.length + ']']: [
              {
                username : '我',
                content: this.data.comment.current_text,
                date: '刚刚'
              },
            ],
            'comment.current_text': ''
          })
        }

        wx.showToast({
          title: resp.data.msg,
          icon: 'none'
        })
      }
    })
  }
})